package business.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import model.TRoleMenu;
import model.VMenu;
import business.basic.iHibBaseDAO;
import business.basic.iHibBaseDAOImpl;
import business.dao.RoleMenuDAO;

@Component("rmenudao")
public class RoleMenuDAOImpl implements RoleMenuDAO {
	
	private iHibBaseDAO bdao = null;

	public RoleMenuDAOImpl() {
		bdao = new iHibBaseDAOImpl();
	}

	public void setBdao(iHibBaseDAO bdao) {
		this.bdao = bdao;
	}

	@Override
	public boolean listadd(List list) {
		return bdao.insert(list);
	}

	@Override
	public int add(TRoleMenu rolemenu) {
		Object obj = bdao.insert(rolemenu);
		if(obj!=null){
			return Integer.parseInt(obj.toString());
		}
		return 0;
	}

	@Override
	public boolean update(TRoleMenu rolemenu) {
		return bdao.update(rolemenu);
	}

	@Override
	public boolean delete(int rolemenuid) {
		return bdao.delete(TRoleMenu.class, rolemenuid);
	}

	@Override
	public boolean deleteByRoleId(int roleid) {
		String sql = "delete from T_RoleMenu where roleid = ?";
		Object[] para = {roleid};
		return bdao.delete(sql, para);
	}

	@Override
	public boolean deleteByMenuId(int menuid) {
		String sql = "delete from T_RoleMenu where menuid = ?";
		Object[] para = {menuid};
		return bdao.delete(sql, para);
	}

	@Override
	public TRoleMenu getTRoleMenuById(int rolemenuid) {
		return (TRoleMenu)bdao.findById(TRoleMenu.class, rolemenuid);
	}

	@Override
	public List<TRoleMenu> getTRoleMenu() {
		String hql = "from TRoleMenu";
		return bdao.select(hql);
	}

	@Override
	public List<TRoleMenu> getTRoleMenuByWhere(String wherecondition,
			int startPage, int pageSize) {
		String hql = "from TRoleMenu";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounTRoleMenuByWhere(String wherecondition) {
		String hql = "select count(*) from TRoleMenu ";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

	@Override
	public List<VMenu> getVMenuByWhere(String wherecondition, int startPage,
			int pageSize) {
		String hql = "from VMenu";
		if(wherecondition!=null && !wherecondition.equals("")){
			hql += wherecondition;
		}
		return bdao.selectByPage(hql, startPage, pageSize);
	}

	@Override
	public int getAmounVMenuByWhere(String wherecondition) {
		String hql = "select count(*) from VMenu ";
		if (wherecondition != null && !wherecondition.equals("")) {
			hql += wherecondition;
		}
		return bdao.selectValue(hql);
	}

}
